home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_066 / free / free.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  224 lines

  1. /*********************************************************************/         
  2. /*                                                                   */   
  3. /*    FREE Copyright 1987 by Tom Smythe                              */
  4. /*         RealTime Asc. Seattle, Wa.                                */
  5. /*         (206)-547-7292                                            */
  6. /*         (206)-774-4735                                            */
  7. /*                                                                   */
  8. /*    Returns free bytes of memory available from specified drives   */
  9. /*                                                                   */                                                               
  10. /*                                                                   */
  11. /*********************************************************************/         
  12.  
  13. #include <libraries/dos.h>
  14. #include <libraries/dosextens.h>
  15. #include <exec/types.h>cc
  16. #include <exec/memory.h>
  17. #include <functions.h>
  18.  
  19. #define   OFFSET    0x2eL
  20. #define   ARGS     "bad arguments\n"
  21. #define   IN       "need full path\n"
  22. #define   READ     "list too long ?\n"
  23. #define   WRITE    "write error\n"
  24. #define   CLEAR    "list cleared\n"
  25. #define   DONE     "list updated\n"
  26. #define   BUFF     "out of memory\n"
  27. #define   STOP     "Real"
  28. #define   FREE     "Time"
  29. #define   USAGE    "FREE Copyright 1987 by Tom Smythe\n\
  30.      RealTime Asc. Seattle, Wa.\n\
  31.      (206)-547-7292\n\
  32.      (206)-774-4735 bbs\n\n\
  33.      FREE    returns available memory.\n\
  34.      <full path>/FREE -x <device list> updates list.\n\
  35.      <full path>/FREE -x clears list.\n"  
  36.      
  37. struct FileHandle *    output       =  NULL;
  38. struct FileHandle *    comfile      =  NULL;
  39.  
  40. struct Lock *          lock         =  NULL;
  41. struct Lock *          mylock       =  NULL;
  42.  
  43. char *                 buf          =  NULL;
  44. char *                 msg          =  NULL;
  45. char                   outline[256] =  NULL;           
  46. char                   *drive[7]    = 
  47.                                     {"DF0:","DF1:","VD0:",FREE,FREE,FREE,STOP};
  48.  
  49. /***************************/ 
  50.  
  51. main(argc,argv)
  52.  
  53.    int argc;
  54.    char *argv[];
  55.  
  56.    {
  57.    char * getfree();
  58.    char * control();
  59.    
  60.    output = Output();
  61.    
  62.    if (*(argv[1]) == '?') Write(output,USAGE,250L); 
  63.       else  if (argc == 1) getfree(outline);
  64.          else strcpy(outline,control(argc,argv));
  65.          Write(output,outline,(long)strlen(outline));
  66.    }
  67.    
  68.    
  69.  
  70. /***************************/ 
  71.       
  72.  
  73. char * itos(ord)
  74.    
  75.    long   ord;
  76.    
  77.    {
  78.    static char  ostring[10];
  79.    int          next = 9;
  80.    
  81.    if (!ord) ostring[next--] = '0';
  82.       
  83.    while ((ord+9)/10)
  84.       {      
  85.       ostring[next--] = ord % 10 + 0x30;
  86.       ord /= 10;
  87.       }
  88.    while (next+1) ostring[next--] = 0x20;
  89.    return(ostring);
  90.    }
  91.  
  92. /* ************************* */ 
  93.  
  94. long avail(disk)
  95.  
  96.    struct Lock *         *disk;
  97.    
  98.    {
  99.    struct InfoData *     info;
  100.  
  101.    long bytes = 0L;
  102.  
  103.    info = AllocMem((long)sizeof(struct InfoData),MEMF_PUBLIC);
  104.    Info(disk,info);
  105.    bytes = (info->id_NumBlocks - info->id_NumBlocksUsed) * info->id_BytesPerBlock;
  106.    FreeMem(info,(long)sizeof(struct InfoData));
  107.    return(bytes);
  108.    }
  109.  
  110. /* ************************* */ 
  111.  
  112. char * getfree(outline)
  113.  
  114.       char * outline;
  115.  
  116.       {
  117.              
  118.       long dfree = 0L;
  119.       long cfree = 0L;
  120.       long ffree = 0L;
  121.       int  i = -1;
  122.       
  123.       Forbid();
  124.       cfree = AvailMem(MEMF_CHIP);
  125.       ffree = AvailMem(MEMF_FAST);
  126.       Permit();
  127.       
  128.       strcat(outline,"Chip");
  129.       strcat(outline,itos(cfree));
  130.       if (ffree)
  131.          {
  132.          strcat(outline,"   Fast");
  133.          strcat(outline,itos(ffree));
  134.          }
  135.       strcat(outline,"\n");   
  136.    
  137.       while (*(drive[++i] +3) == ':')
  138.          {
  139.          if (lock = Lock(drive[i],ACCESS_READ)) 
  140.             {
  141.             dfree = avail(lock);
  142.             UnLock(lock);
  143.             if (!dfree)
  144.                {
  145.                if (!strcmp(drive[i],"RAM:")) dfree = ffree + cfree;  
  146.                if (!strcmp(drive[i],"ram:")) dfree = ffree + cfree;
  147.                }
  148.             strcat(outline,"");
  149.             strcat(outline,drive[i]);
  150.             strcat(outline,"");
  151.             strcat(outline,itos(dfree));
  152.             strcat(outline,"\n");   
  153.             }   
  154.          }
  155.       }
  156.       
  157. /* ************************* */ 
  158.  
  159. char * control(Argc,Argv)
  160.  
  161.    int Argc;
  162.    char * Argv[];
  163.    
  164.    {
  165.    char *                       cleanup();
  166.    char *                       msg = NULL;
  167.    int i;
  168.    
  169.    if (strcmp(Argv[1],"-x")) return(ARGS);
  170.    if (Argc > 2)
  171.       for (i=Argc;i>2;i--) 
  172.          if (*(Argv[i-1]+3) != ':') return(ARGS);
  173.  
  174.    if (!(mylock = Lock(Argv[0],ACCESS_READ))) return(IN);
  175.    if (!(comfile = Open(Argv[0],MODE_OLDFILE))) return(cleanup(IN));
  176.       
  177.    if (!(buf = (char *)AllocMem(4L,MEMF_CHIP|MEMF_CLEAR))) return(cleanup(BUFF));
  178.      
  179.    Seek(comfile,OFFSET,OFFSET_BEGINNING);
  180.    
  181.    if (Argc == 2)
  182.       {
  183.       while ((Read(comfile,buf,4L) == 4L) && (strcmp(buf,STOP)))  
  184.          {
  185.          Seek(comfile,-4L,OFFSET_CURRENT);
  186.          Write(comfile,FREE,4L);
  187.          Seek(comfile,1L,OFFSET_CURRENT);
  188.          }
  189.       return(cleanup(strcmp(buf,STOP) ? READ : CLEAR));
  190.       }
  191.    
  192.    while (Argv[i])
  193.  
  194.       {
  195.       if ((Read(comfile,buf,4L) != 4L) || (!(strcmp(buf,STOP))))
  196.          return(cleanup(READ));
  197.          
  198.       if (!strcmp(buf,FREE))
  199.          {
  200.          Seek(comfile,-4L,OFFSET_CURRENT);
  201.          if (Write(comfile,Argv[i],4L) != 4L)
  202.             return(cleanup(WRITE));
  203.          i++;
  204.          }
  205.       Seek(comfile,1L,OFFSET_CURRENT);
  206.       }     
  207.     
  208.    return(cleanup(DONE));
  209.    }
  210.  
  211. /***************************/ 
  212. char * cleanup(err)
  213.  
  214.    char *err;
  215.    
  216.    {
  217.    if (comfile) Close(comfile);
  218.    if (mylock) UnLock(mylock);
  219.    if (buf) FreeMem(buf,4L);
  220.    return(err);
  221.    }
  222.  
  223. /***************************/ 
  224.